﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Model;
using System.Web.UI;
using BLL;
using Comm;

namespace PMS.Web
{
    public class PageComm : Page
    {
        private PMS_User GetUserInfo()
        {
            object o = Session["UserInfo"];
            if (o == null || o.ToString() == "")
            {
                return null;
            }
            else
            {
                PMS_User userInfo = o as PMS_User;
                return userInfo;
            }
        }

        public static PMS_User GetUser(HttpResponse response)
        {
            PageComm com = new PageComm();
            PMS_User user = com.GetUserInfo();
            if (user == null)
            {
                response.Redirect("Login.aspx");
            }
            return user;
        }

        /// <summary>
        /// 是否可以访问
        /// </summary>
        /// <param name="menu"></param>
        /// <param name="role"></param>
        /// <returns></returns>
        public static bool IsVisit(ActionMenu menu, string role)
        {
            if (role == "管理员")
            {
                return true;
            }
            else
            {
                MenuRoles factry = new MenuRoles(menu);
                IMenuRole bll = factry.CreateFactry();
                return bll.IsVisit(role);
            }
        }

        /// <summary>
        /// 是否可编辑
        /// </summary>
        /// <param name="menu"></param>
        /// <param name="role"></param>
        /// <returns></returns>
        public static bool IsEdit(ActionMenu menu, string role)
        {
            if (role == "管理员")
            {
                return true;
            }
            else
            {
                MenuRoles factry = new MenuRoles(menu);
                IMenuRole bll = factry.CreateFactry();
                return bll.IsEdit(role);
            }
        }

        public static List<PMS_RoleList> GetCacheListLoad()
        {
            PageComm com = new PageComm();
            PMS_User user = com.GetUserInfo();
            PMS_RoleListBLL bll = new BLL.PMS_RoleListBLL();
            List<PMS_RoleList> list = new List<PMS_RoleList>();
            if (user != null)
            {
                string key = "GetCacheListPMS_RoleList" + user.id;
                if (user.roleID == 0)
                {
                    list = bll.GetList();
                }
                else
                {
                    list = bll.GetList(user.roleID);
                }
                ClientCache.Add(key, list);
            }
            return list;
        }
        /// <summary>
        /// 查询用户数据
        /// </summary>
        /// <returns></returns>
        public static List<PMS_RoleList> GetCacheList()
        {
            PageComm com = new PageComm();
            PMS_User user = com.GetUserInfo();
            PMS_RoleListBLL bll = new BLL.PMS_RoleListBLL();
            List<PMS_RoleList> list = new List<PMS_RoleList>();
            if (user != null)
            {
                string key = "GetCacheListPMS_RoleList" + user.id;
                list = ClientCache.Get<List<PMS_RoleList>>(key);
                if (list == null)
                {
                    if (user.roleID == 0)
                    {
                        list = bll.GetList();
                    }
                    else
                    {
                        list = bll.GetList(user.roleID);
                    }
                    ClientCache.Add(key, list);
                }
            }
            return list;
        }
    }
}